<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/14
 * Time: 11:02
 */
namespace app\admin\builder;

use app\common\model\Rule;
use think\Controller;

/**
 * AdminBuilder：快速建立管理页面。
 *
 * 这里并没有继承admincontroller，将获取模块以及菜单
 *
 * Class AdminBuilder
 * @package Admin\Builder
 */


class AdminBuilder extends Controller {

    /**
     * 初始化方法中添加 menu 以及 module_menu
     */
    public function _initialize()
    {
        parent::_initialize();

        $this->assign('menu', $this->getMenus());
        $this->assign('choice',$this->getChoice());
        $this->assign('breadcrumbs',$this->getBreadcrumbs());
        $this->assign('request_url',strtolower($this->request->path()));
        header('X-Powered-By: gdu-tech.com');
    }

    public function display($templateFile='',$charset='',$contentType='',$content='',$prefix='') {

        $template = dirname(__FILE__) . '/../view/Builder/' . $templateFile . '.html';
        return $this->fetch($template);
    }

    protected function compileHtmlAttr($attr) {
        $result = array();
        foreach($attr as $key=>$value) {
            $value = htmlspecialchars($value);
            $result[] = "$key=\"$value\"";
        }
        $result = implode(' ', $result);
        return $result;
    }


    /**
     * 获取模块列表，用于显示在左侧
     */
   final  protected function getModules()
    {

        return [];
    }

    /**
     * 获取控制器菜单数组,二级菜单元素位于一级菜单的'_child'元素中
     *
     */
    final protected function getMenus()
    {
        $data  = model('Rule')->getMenus();
        return  $data;
    }

    /**
     *
     * 获取当前选中菜单
     * todo 仅支持二级菜单目录
     */
    final protected  function getChoice(){

        return Rule::menuId($this->request->path());
    }

    final protected function getBreadcrumbs(){

     return Rule::Breadcrumbs($this->request->path());
   }


}